﻿using Framework.Core.JsonSerialization.Converters;

namespace Framework.Core.JsonSerialization.Extensions
{
	/// <summary>
	/// Newtonsoft.Json 拓展
	/// </summary>
	public static class NewtonsoftJsonExtensions
	{
		/// <summary>
		/// Newtonsoft.Json 扩展配置
		/// </summary>
		/// <typeparam name="TJsonSerializerProvider"></typeparam>
		/// <param name="iMvcBuilder"></param>
		/// <param name="Services"></param>
		/// <param name="setupAction"></param>
		public static void AddNewtonsoftJsonExtensions<TJsonSerializerProvider>(this IMvcBuilder iMvcBuilder, 
			IServiceCollection Services, Action<MvcNewtonsoftJsonOptions> setupAction = default)
			 where TJsonSerializerProvider : class, IJsonSerializerProvider
		{
			// 注入自定义的Json序列化提供器
			Services.AddSingleton<IJsonSerializerProvider, TJsonSerializerProvider>();

			// 使用Newtonsoft.Json序列化配置
			iMvcBuilder.AddNewtonsoftJson(setupAction);
		}

		/// <summary>
		/// 添加 Newtonsoft.Json 配置
		/// </summary>
		/// <param name="iMvcBuilder"></param>
		/// <param name="Services"></param>
		/// <param name="setupAction"></param>
		public static void AddNewtonsoftJsonExtensions(this IMvcBuilder iMvcBuilder,IServiceCollection Services, 
			Action<MvcNewtonsoftJsonOptions> setupAction = default)
		{
			// 注入默认的Json序列化提供器
			Services.AddSingleton<IJsonSerializerProvider, NewtonsoftJsonSerializerProvider>();

			// 使用Newtonsoft.Json序列化配置
			iMvcBuilder.AddNewtonsoftJson(setupAction);
		}

		/// <summary>
		/// 添加 Newtonsoft.Json 配置
		/// </summary>
		/// <param name="iMvcBuilder"></param>
		/// <param name="Services"></param>
		public static void AddNewtonsoftJsonExtensions(this IMvcBuilder iMvcBuilder, IServiceCollection Services)
		{
			// 注入默认的Json序列化提供器
			Services.AddSingleton<IJsonSerializerProvider, NewtonsoftJsonSerializerProvider>();

			// 使用Newtonsoft.Json序列化配置
			iMvcBuilder.AddNewtonsoftJson();
		}

		/// <summary>
		/// 添加 long/long? 类型序列化处理
		/// </summary>
		/// <remarks></remarks>
		public static IList<JsonConverter> AddLongTypeConverters(this IList<JsonConverter> converters)
		{
			converters.Add(new NewtonsoftJsonLongToStringJsonConverter());
			converters.Add(new NewtonsoftJsonNullableLongToStringJsonConverter());

			return converters;
		}

		/// <summary>
		/// 添加 DateOnly/DateOnly? 类型序列化处理
		/// </summary>
		/// <param name="converters"></param>
		/// <returns></returns>
		public static IList<JsonConverter> AddDateOnlyConverters(this IList<JsonConverter> converters)
		{
#if !NET5_0
			converters.Add(new NewtonsoftJsonDateOnlyJsonConverter());
			converters.Add(new NewtonsoftJsonNullableDateOnlyJsonConverter());
#endif
			return converters;
		}

		/// <summary>
		/// 添加 TimeOnly/TimeOnly? 类型序列化处理
		/// </summary>
		/// <param name="converters"></param>
		/// <returns></returns>
		public static IList<JsonConverter> AddTimeOnlyConverters(this IList<JsonConverter> converters)
		{
#if !NET5_0
			converters.Add(new NewtonsoftJsonTimeOnlyJsonConverter());
			converters.Add(new NewtonsoftJsonNullableTimeOnlyJsonConverter());
#endif
			return converters;
		}
	}
}
